from sqlalchemy.exc import IntegrityError

from dc_shop.database.exception_mapper import exception_mapper
from dc_shop.database.models import Review
from dc_shop.database.repositories.base import SQLAlchemyRepo


class ReviewRepoImpl(SQLAlchemyRepo):
    @exception_mapper
    async def create_review(self, review: Review):
        self._session.add(review)
        try:
            await self._session.flush((review,))
        except IntegrityError as err:
            raise err

    @exception_mapper
    async def update_review(self, review: Review) -> None:
        try:
            await self._session.merge(review)
        except IntegrityError as err:
            raise err
